package net.xi.dmhome.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xi.dmhome.entity.*;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.PostMapper;
import net.xi.dmhome.mapper.CommentMapper;
import net.xi.dmhome.mapper.PosttypeMapper;
import net.xi.dmhome.mapper.UserMapper;
import net.xi.dmhome.service.PostService;
import net.xi.dmhome.service.CommentService;
import net.xi.dmhome.service.PosttypeService;
import net.xi.dmhome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jxhu
 * @Description:论坛控制层
 * @Date Created in 2021-02-07 18:03
 * @Modified By:
 */

@RestController
@RequestMapping("/forum")
public class ForumController {
    @Autowired
    PostMapper postMapper;
    @Autowired
    PostService postService;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CommentService commentService;
    @Autowired
    PosttypeService posttypeService;
    @Autowired
    UserMapper userMapper;

    /* @Description: 帖子信息添加,@Param:  */
    @PostMapping("/addPost")
    public Result addPost(@Validated @RequestBody(required = false) Post post) throws Exception {

        String userid = post.getUserid();
        User user = userMapper.selectByUserUUid(userid);
        Integer postnumber = user.getPostnumber();
        user.setPostnumber(++postnumber);
        userMapper.updateById(user);
        int insert = postMapper.insert(post);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 根据id查找帖子,@Param:  */
    @GetMapping("/findPostbyid/{id}")
    public Result getPostById(@PathVariable("id") Integer id) throws Exception {
        Post post = postMapper.selectById(id);


        return Result.success(post);

    }
    /* @Description: 编辑帖子,@Param:  */
    @PutMapping("/editPost")
    public Result editPost(@Validated @RequestBody Post post) throws Exception {
        boolean update = postService.saveOrUpdate(post);
        if (update !=false) {

            System.out.println("更改成功");
            return Result.success(200, "更改成功", post);
        } else {
            return Result.fail("更改失败");
        }
    }


    /* @Description: 删除帖子,@Param:  */
    @DeleteMapping("/deletePost/{id}")
    public Result deletePost(@PathVariable("id") Integer id){
        int i = postMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }
    /* @Description: 减少用户帖子发布数,@Param:  */
    @PutMapping("/postDown/{uuid}")
    public Result postDown(@PathVariable("uuid") String uuid){
        User user = userMapper.selectByUserUUid(uuid);
        Integer postnumber = user.getPostnumber();
        postnumber = postnumber-1;
        user.setPostnumber(postnumber);
        int i = userMapper.updateById(user);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }
    /* @Description: 帖子过审,@Param:  */
    /* @Description: 过审,@Param:  */
    @PutMapping("/past/{id}")
    public Result pastPost(@PathVariable("id") Integer id){
        Post post = postMapper.selectById(id);
        post.setIspast(1);
        postService.saveOrUpdate(post);
        return Result.success("success");
    }

    /* @Description: 不过审,@Param:  */
    @PutMapping("/fail/{id}")
    public Result failQuestion(@PathVariable("id") Integer id){
        Post post = postMapper.selectById(id);
        post.setIspast(0);
        postService.saveOrUpdate(post);
        return Result.success("success");
    }

        
    @GetMapping("/listtype")
    public Result listype(){
        List<Posttype> list = posttypeService.list();
        return Result.success(list);

    }

    /* @Description: 根据分类查找帖子,@Param:  */
    @GetMapping("/listPosttype")
    public Result listPostType(@RequestParam(defaultValue = "1") Integer currentPage,String type) {

        Page page = new Page(currentPage, 6);
        IPage iPage = postService.page(page, new QueryWrapper<Post>().eq("type",type).
                eq("ispast",1).
                orderByDesc("createtime"));

        return Result.success(iPage);

    }

    /* @Description: 根据用户id查找对应的帖子,@Pauram:  */
    @GetMapping("/findPostbyuid/{uuid}")
    public Result getPostByUUid(@PathVariable("uuid") String uuid) throws Exception {
        List<Post> posts = postMapper.selectByuserId(uuid);


        return Result.success(posts);

    }
}
